<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/9/18
  Time: 19:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../include/head.jsp">
    <jsp:param name="title" value="租户服务管理"/>
</jsp:include>

<jsp:include page="../include/admin_header.jsp"/>

<jsp:include page="../include/admin_sidebar.jsp"/>


<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.tenant.service.manage"/> </h4>

        <div class="sie-search-parent">
            <input type="text" placeholder="<spring:message code="label.admin.user.list.search"/>" class="form-control sie-search-input" id="inputSuccess2">
            <img src="/images/search.png" class="sie-search-img"/>
        </div>
    </section>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body" style="padding: 0">
                <table class="table" id="service_list">
                    <thead>
                    <tr>
                        <th><spring:message code="label.tenant.service.tenant.name"/> </th>
                        <th><spring:message code="label.tenant.service"/> </th>
                        <th><spring:message code="label.tenant.create.period"/> </th>
                        <th><spring:message code="label.tenant.service.state"/> </th>
                        <th><spring:message code="label.tenant.register"/> </th>
                        <th><spring:message code="label.admin.user.list.operation"/></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        var options ={
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: true,
            fixedHeader: true,
            searching: true,
            "processing": true,
            aLengthMenu: [15],
            ajax: {
                url: '/admin/api/v1/service/managesByDataTable',
                dataSrc: 'data'
            },
            columns: [
                {
                    data: 'tenant_name'
                },
                {
                    data: 'serviceName',
                    "render": function (data, type, full, meta) {
                        if (data==null){
                            return '当前无服务';
                        }
                        var names = data.split(",");
                        var id = full.serviceId.split(",");
                        var divString = $("<div></div>");
                        for(var i = 0;i<names.length;i++){
                            var _name = "<div class='name' data-id='"+id[i]+"'>" + names[i] + "</div>";
                            divString.append(_name);
                        }
                        return divString.html();
                    }
                },
                {
                    data: 'valid',
                    "render": function (data, type, full, meta) {
                        if (data==null){
                            return '当前无服务';
                        }
                        var times = data.split(",");
                        var divString = $("<div></div>");
                        $.each(times, function (index, time) {
                            var _time = "<div>" + time + "</div>";
                            divString.append(_time);
                        });
                        return divString.html();
                    }
                },
                {
                    data: 'enabled',
                    "render": function (data, type, full, meta) {
                         if (data==null){
                             return '当前无服务';
                         }
                        var enabledArr = data.split(",");
                        var divString = $("<div></div>");
                        $.each(enabledArr, function (index, enabled) {
                            var _enabled;
                            if (enabled == 0) {
                                _enabled = "<div><spring:message code="label.tenant.service.not.available"/></div>";
                                divString.append(_enabled);
                            } else if (enabled == 1) {
                                _enabled = "<div><spring:message code="label.tenant.service.available"/></div>";
                                divString.append(_enabled);
                            }
                        });
                        return divString.html();
                    }
                },
                {
                    data: 'count'
                },
                {
                    data: 'tenant_id',
                    "render": function (data, type, full, meta) {
                        var divString = '<div class="sie-btn-group">' +
                                '<a href="javascript:;" onclick="serviceContract(this,\''+data+'\')" style="margin-left: 0;"><i class="sie-btn-edit"></i>' +
                                '<span><spring:message code="label.service.postpone"/> </span>' +
                                '</a>' +
                                '</div>';
                        return divString;
                    }
                }
            ]
        };
        var uri = window.location.href;
        console.log(uri);
        if (!(uri.indexOf("lang=en")>0)) {
           options["language"]={url: '/lang/datatable.chs.json'};
        }
        var table = $('#service_list').DataTable(options);
        $('#inputSuccess2').bind('keyup', function (e) {
            table.search(this.value).draw();
        });
    });
    function  serviceContract(_this,tenantId){
        var service = $(_this).parents("tr").find(".name");
        var serviceId="";
        $.each(service,function(i,id){
            serviceId = serviceId+$(id).data("id")+",";
        });
        serviceId = serviceId.substr(0,serviceId.length-1);

        window.location.href="/admin/service/manageEdit?tenantId="+tenantId+"&serviceId="+serviceId+"#serviceManage";
    }
</script>
